package com.starblink.wishlist.wishboards.ui.cells

import android.app.Activity
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.aroute.AccountProvider
import com.starblink.android.basic.const.NavParamsKeyConst
import com.starblink.android.basic.ext.*
import com.starblink.android.basic.extension.navigation
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.user.LoginCheckInterceptor
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.ImageUtils
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.databinding.BaseLayoutEmptyWishTemplateBinding
import com.starblink.basic.util.CommUtils
import com.starblink.basic.util.log.YYLogUtils
import com.starblink.library.widget.databinding.CellSimpleProductCardBinding
import com.starblink.library.widget.product.ProductCardScene
import com.starblink.rocketreserver.FetchAccountInfoQuery
import com.starblink.rocketreserver.fragment.ProductF
import com.starblink.wishlist.R
import com.starblink.wishlist.databinding.ItemAllItemTotalBinding
import com.starblink.wishlist.databinding.ItemBoardChildBinding
import com.starblink.wishlist.databinding.LayoutBoardCreateBinding
import com.starblink.wishlist.databinding.LayoutRelatedTitleBinding
import com.starblink.wishlist.wishallitems.ui.bean.RvTotalNumEntry
import com.starblink.wishlist.wishboards.ui.bean.*


/**
 * feeds无数据
 */
class EmptyBoardsCell(
    private val activity: Activity,
    binding: BaseLayoutEmptyWishTemplateBinding
) :
    BaseBindingVH<RvNoBoardsEntry, BaseLayoutEmptyWishTemplateBinding>(binding) {


    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
    }

    override fun onBind(model: RvNoBoardsEntry, position: Int) {
        binding.root.post {
            binding.root.setEmptyViewForWishTemplate(
                title = "Create Boards to manage \nWishlists",
                buttonTxt = "+ Create a new Board",
//                centerParentViewHeight = 308.dp,
                marginTop = 22.dp,//32DP 因为此界面放到了recycleView里 默认有10的间距
                marginBottom = 32.dp,
                onBtnClick = {
                    LoginCheckInterceptor.check2Login(activity) {
                        activity.navigation(path = RoutePage.Wishlist.CREATE_BOARD)
                    }
                    SpmTrackHandler.addSingleTrackData(
                        SkAntEntity(
                            spmCnt = GTrackerAssistUtils.fetchElementValue(
                                SpmPageDef.Page_10209,
                                SpmElementDef.WishListCreateClick
                            )
                        )
                    )
                }
            )
        }
    }
}


class AddBoardsCell(
    private val activity: Activity,
    binding: LayoutBoardCreateBinding
) :
    BaseBindingVH<RvAddBoardsEntry, LayoutBoardCreateBinding>(binding) {


    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
    }

    override fun onBind(model: RvAddBoardsEntry, position: Int) {
        binding.emptyButton.setOnClickListener {
            LoginCheckInterceptor.check2Login(activity) {
                activity.navigation(path = RoutePage.Wishlist.CREATE_BOARD)
            }
        }
        binding.emptyButton.trackData(
            page = SpmPageDef.Page_10209,
            element = SpmElementDef.WishListCreateClick
        )
    }
}


class BoardItemsCell(
    private val activity: Activity,
    binding: ItemBoardChildBinding,
    var choiceClickCallback: ((
        position: Int, isChoice: Boolean,
    ) -> Unit)? = null,
    var deleteClickCallback: ((
        model: RvBoardItemEntry, position: Int
    ) -> Unit)? = null
) :
    BaseBindingVH<RvBoardItemEntry, ItemBoardChildBinding>(binding) {


    private val defaultColor = CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray100)
    private val defaultColorRes = com.starblink.basic.style.R.color.sk_gray100

    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
    }

    override fun onBind(model: RvBoardItemEntry, position: Int) {
        binding.root.setData(
            showChoiceMode = model.showCheckMode,
            hasChoice = model.hasChoice,
            showSaveBtn = false,
            wishListItem = model.wishItem,
            position = position,
            choiceClickCallback = { isChoice ->
                choiceClickCallback?.invoke(position, isChoice)
            },
            deleteClickCallback = {
                deleteClickCallback?.invoke(model, position)
            },
            itemClickCallback = {
                UserDataCenter.getAccountInfo()?.let {
                    activity.navigationTo(RoutePage.Wishlist.WISHLIST_DETAILS) {
                        withBoolean(NavParamsKeyConst.WISHLIST_IS_MASTER, true)
                        withString(NavParamsKeyConst.WISHLIST_USER_ID, it.id)
                        withString(NavParamsKeyConst.WISHLIST_USER_IMG, it.headImage)
                        withString(NavParamsKeyConst.WISHLIST_USER_NAME, it.userName)
                        withObject(NavParamsKeyConst.WISHLIST_BOARD_ITEM, model.wishItem)
                    }
                }
            },
            spmPageValue = SpmPageDef.Page_10209
        )
    }
}

class BoardRelatedTitleCell(
    binding: LayoutRelatedTitleBinding
) :
    BaseBindingVH<RvRelatedTitleEntry, LayoutRelatedTitleBinding>(binding) {


    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
    }

    override fun onBind(model: RvRelatedTitleEntry, position: Int) {
    }
}


class WishProductCell(
    private val refreshLayout: SmartRefreshLayout,
    private val recyclerView: RecyclerView,
    var wishListId: String? = null,
    binding: CellSimpleProductCardBinding,
    var spmPageValue: Int,
    val cardClick: (md: RvBoardProductEntry, p: Int) -> Unit
) :
    BaseBindingVH<RvBoardProductEntry, CellSimpleProductCardBinding>(binding) {

    private companion object {
        const val TAG = "WishProductCell"
    }

    override fun onBind(model: RvBoardProductEntry, position: Int) {
        binding.card.scene = ProductCardScene.WISH_LIST
        binding.card.bindProduct(
            refreshLayout = refreshLayout,
            recyclerView =recyclerView,
            wishListId = wishListId,
            productWrap = model,
            spmPageValue = spmPageValue,
            guideType = TAG
        )
    }
}


class WishTotalCell(
    binding: ItemAllItemTotalBinding
) :
    BaseBindingVH<RvTotalNumEntry, ItemAllItemTotalBinding>(binding) {
    init {
        itemView.layoutParams = StaggeredGridLayoutManager.LayoutParams(
            StaggeredGridLayoutManager.LayoutParams.MATCH_PARENT,
            StaggeredGridLayoutManager.LayoutParams.WRAP_CONTENT
        ).apply {
            isFullSpan = true
        }
    }

    override fun onBind(model: RvTotalNumEntry, position: Int) {
        binding.totalNums.text = "${model.total} Items"
    }
}










